//
//  TPAskanswerSinglePageView.m
//  ThePaperBase
//
//  Created by liubo on 2021/8/25.
//  Copyright © 2021 scar1900. All rights reserved.
//

#import "TPAskanswerSinglePageView.h"
#import "TPAskAnswerRefreshView.h"
#import <Masonry/Masonry.h>
#import "LBVerticalPageViewLoadMoreView.h"

@interface TPAskanswerSinglePageView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) TPAskAnswerRefreshView *refreshIndicatorView;

@property (nonatomic, strong) LBVerticalPageViewLoadMoreView *loadMoreIndicatorView;

@end

@implementation TPAskanswerSinglePageView

@synthesize refreshIndicatorView = _refreshIndicatorView;
@synthesize loadMoreIndicatorView = _loadMoreIndicatorView;

- (void)setUpUI
{
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView addSubview:self.refreshIndicatorView];
    [self.refreshIndicatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(375);
        make.top.mas_equalTo(-50);
        make.height.mas_equalTo(50);
    }];
}

- (void)updateWithModel:(id)model
{
    self.dataArray = model;
    [self.tableView reloadData];
    self.tableView.contentOffset = CGPointMake(0, 0);
//    [self.tableView reloadData];
}

#pragma mark - UItabelviewDatasource UItabelviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    NSString *content = self.dataArray[indexPath.row];
    cell.textLabel.text = content;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

#pragma mark - lazy load

- (TPAskAnswerRefreshView *)refreshIndicatorView
{
    if (!_refreshIndicatorView) {
        _refreshIndicatorView = [[TPAskAnswerRefreshView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.bounds), 50)];
        [_refreshIndicatorView configTitle:@"下拉看上一个问答" forState:UIControlStateNormal];
        [_refreshIndicatorView configTitle:@"松开查看" forState:UIControlStateSelected];
        [_refreshIndicatorView configTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        [_refreshIndicatorView configImage:[[UIImage alloc] init] forState:UIControlStateSelected];
        [_refreshIndicatorView configTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    }
    return _refreshIndicatorView;
}

- (UIButton *)loadMoreIndicatorView
{
    if (!_loadMoreIndicatorView) {
        _loadMoreIndicatorView = [LBVerticalPageViewLoadMoreView buttonWithType:UIButtonTypeCustom];
        _loadMoreIndicatorView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds), 80);
        [_loadMoreIndicatorView setTitle:@"上拉看下一个问答：" forState:UIControlStateNormal];
        [_loadMoreIndicatorView setTitle:@"松手查看" forState:UIControlStateSelected];
        [_loadMoreIndicatorView setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [_loadMoreIndicatorView setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
        [_loadMoreIndicatorView setImage:[[UIImage alloc] init] forState:UIControlStateSelected];
        _loadMoreIndicatorView.titleLabel.font = [UIFont systemFontOfSize:16];
        _loadMoreIndicatorView.userInteractionEnabled = NO;
    }
    return _loadMoreIndicatorView;
}

@end
